package com.example.testrxjava.util;

import android.content.Context;
import android.util.Log;

import com.example.testrxjava.activity.search.HotKeyBean;
import com.example.testrxjava.activity.search.HotSearchApi;
import com.example.testrxjava.activity.search.HotSearchBean;
import com.example.testrxjava.activity.search.HotSearchSucessCallback;
import com.example.testrxjava.activity.search.HotkeyApi;
import com.example.testrxjava.activity.search.SearchSucessCallback;
import com.example.testrxjava.base.MyApplication;
import com.example.testrxjava.fragment.article.ArticleListApi;
import com.example.testrxjava.fragment.article.ArticleListBean;
import com.example.testrxjava.fragment.article.ArticleListSucessCallback;
import com.example.testrxjava.fragment.code.WxArticleBean;
import com.example.testrxjava.fragment.code.WxArticleSApi;
import com.example.testrxjava.fragment.code.WxArticleSBean;
import com.example.testrxjava.fragment.code.WxArticleSSucessCallback;
import com.example.testrxjava.fragment.code.WxArticleSucessCallback;
import com.example.testrxjava.fragment.code.WxarticleApi;
import com.example.testrxjava.fragment.home.BannerBean;
import com.example.testrxjava.fragment.home.HomeBannerApi;
import com.example.testrxjava.fragment.home.HomeBannerSucessCallBack;
import com.example.testrxjava.fragment.home.HomeListApi;
import com.example.testrxjava.fragment.home.HomeListBean;
import com.example.testrxjava.fragment.home.HomeListSucessCallback;
import com.example.testrxjava.fragment.person.LogoutApi;
import com.example.testrxjava.fragment.person.LogoutBean;
import com.example.testrxjava.fragment.person.LogoutSucessCallback;
import com.example.testrxjava.fragment.project.ProjectApi;
import com.example.testrxjava.fragment.project.ProjectBean;
import com.example.testrxjava.fragment.project.ProjectInfoApi;
import com.example.testrxjava.fragment.project.ProjectInfoBean;
import com.example.testrxjava.fragment.project.ProjectInfoSucessCallback;
import com.example.testrxjava.fragment.project.ProjectTreeSucessCallback;
import com.example.testrxjava.fragment.system.KnowLedgeApi;
import com.example.testrxjava.fragment.system.KnowLedgeBean;
import com.example.testrxjava.fragment.system.KnowLedgeSucessCallback;
import com.example.testrxjava.login.LoginApi;
import com.example.testrxjava.login.LoginBean;
import com.example.testrxjava.login.LoginSucessCallBack;
import com.example.testrxjava.register.RegisterApi;
import com.example.testrxjava.register.RegisterBean;
import com.example.testrxjava.register.RegisterSucessCallBack;

import java.util.Map;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 项目名称：TestRxjava
 *
 * @Author 郭迎辉
 * Create by 14:00 on 2019/4/17.
 * Describe:
 */
public class Action {
    //登陆

    public  void login(Context context, Map<String,String> map, final LoginSucessCallBack callBack){
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        OkHttpClient client = builder.addInterceptor(new SaveCookiesInterceptor(context))
                .build();

        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ConTract.base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();

        LoginApi api=retrofit.create(LoginApi.class);
        api.login(map)
                .subscribeOn(Schedulers.io())//在IO线程交互//被观察者
                .observeOn(AndroidSchedulers.mainThread())//主线程//观察者
                .subscribe(new Observer<LoginBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callBack.fail(e.getMessage());
                    }

                    @Override
                    public void onNext(LoginBean bean) {
                        callBack.sucess(bean);
                    }
                });

    }




    //注册
    private static Action instance;
    public static Action getInstance(){
        if(instance==null){
            instance=new Action();
        }
        return instance;
    }
    public  void register(Map<String,String> map, final RegisterSucessCallBack callBack){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ConTract.base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        RegisterApi api=retrofit.create(RegisterApi.class);
        api.register(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<RegisterBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callBack.fail(e.getMessage());

//                        Log.e("erreo",e.getMessage());
                    }

                    @Override
                    public void onNext(RegisterBean bean) {
                        callBack.sucess(bean);
//                        Log.e("===",bean.getData().getUsername());
//                        Log.e("===",bean.getData().getEmail());
//                        Log.e("===",bean.getData().getPassword());
//                        Log.e("===",bean.getData().getId()+"");
//                        Log.e("===",bean.getData().getType()+"");
                    }
                });


    }

    //获取首页banner
    public void getHomeBanner(final HomeBannerSucessCallBack callBack){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ConTract.base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        HomeBannerApi api=retrofit.create(HomeBannerApi.class);
        api.getHomeBanner()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BannerBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callBack.bannerFail(e.getMessage());
                    }

                    @Override
                    public void onNext(BannerBean bean) {
                        callBack.bannerSucess(bean);
                    }
                });




    }

    //获取首页文章列表
    public void  getHomeList(int page, final HomeListSucessCallback callback){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ConTract.base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        HomeListApi api=retrofit.create(HomeListApi.class);
        api.getHomeList(page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HomeListBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.homeListFail(e.getMessage());
                    }

                    @Override
                    public void onNext(HomeListBean bean) {
                        callback.homeListSucess(bean);
                    }
                });

    }
//获取热门搜索

    public void getHotKey(final SearchSucessCallback callback){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ConTract.base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        HotkeyApi api=retrofit.create(HotkeyApi.class);
        api.getHotKey()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HotKeyBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.searchFail(e.getMessage());
                    }

                    @Override
                    public void onNext(HotKeyBean bean) {
                        callback.searchSucess(bean);
                    }
                });



    }


    //获取知识体系
    public void getKnowLedgeTree(final KnowLedgeSucessCallback callback){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ConTract.base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        KnowLedgeApi api=retrofit.create(KnowLedgeApi.class);
        api.getKnowLedgeTree()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<KnowLedgeBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.systemFail(e.getMessage());
                    }

                    @Override
                    public void onNext(KnowLedgeBean bean) {
                        callback.systemSucess(bean);
                    }
                });


    }

//    //获取体系下的文章列表
//
    public void getArticleList(String cid, int page, final ArticleListSucessCallback callback){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ConTract.base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        ArticleListApi api=retrofit.create(ArticleListApi.class);
        api.getSystemTreeArticleList(page,cid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ArticleListBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.systemTreeAcFail(e.getMessage());
                    }

                    @Override
                    public void onNext(ArticleListBean articleListBean) {
                        callback.systemTreeAcSucess(articleListBean);
                    }
                });
    }

    //获取公众号列表
    public void getWxArticleList(final WxArticleSucessCallback callback){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ConTract.base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        WxarticleApi api=retrofit.create(WxarticleApi.class);
        api.getWxArticle()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<WxArticleBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.wxArticleFail(e.getMessage());
                    }

                    @Override
                    public void onNext(WxArticleBean bean) {
                        callback.wxArticleSucess(bean);
                    }
                });
    }
    //获取公众号文章
    public void getWxArticleSList(String id, int page, final WxArticleSSucessCallback callback){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ConTract.base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        WxArticleSApi api=retrofit.create(WxArticleSApi.class);
        api.getWxArticleSList(id,page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<WxArticleSBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.wxArticleSFail(e.getMessage());
                    }

                    @Override
                    public void onNext(WxArticleSBean bean) {
                        callback.wxArticleSSucess(bean);
                    }
                });


    }
    //获取项目分类
    public void getProjectTree(final ProjectTreeSucessCallback callback){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ConTract.base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        ProjectApi api=retrofit.create(ProjectApi.class);
        api.getProjectTree()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ProjectBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.projectFail(e.getMessage());
                    }

                    @Override
                    public void onNext(ProjectBean bean) {
                        callback.projectSucess(bean);
                    }
                });



    }
    //获取项目列表
    public void getProjectInfoList(int page, String cid, final ProjectInfoSucessCallback callback){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ConTract.base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        ProjectInfoApi api=retrofit.create(ProjectInfoApi.class);
        api.getProjectInfoTree(page, cid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ProjectInfoBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.projectInfoFail(e.getMessage());
                    }

                    @Override
                    public void onNext(ProjectInfoBean bean) {
                        callback.projectInfoSucess(bean);
                    }
                });


    }


    //热搜详情

    public void hotSeachInfo(int page, String hot, final HotSearchSucessCallback callback){
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ConTract.base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();

        HotSearchApi api=retrofit.create(HotSearchApi.class);
        api.hotSearchList(page,hot)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HotSearchBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        callback.hotSearchFail(e.getMessage());
                    }

                    @Override
                    public void onNext(HotSearchBean bean) {
                        callback.hotSeachSucess(bean);
                    }
                });

    }


    //退出登陆

    public void logout(Context context, final LogoutSucessCallback callback){
        OkHttpClient.Builder builder=new OkHttpClient.Builder();
        OkHttpClient client=builder.addInterceptor(new AddCookiesInterceptor(context))
                .build();
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ConTract.base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();
        LogoutApi api=retrofit.create(LogoutApi.class);
        api.logout().subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LogoutBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("退出登陆==",e.getMessage());
                        callback.logoutFail(e.getMessage());
                    }

                    @Override
                    public void onNext(LogoutBean s) {
//                        Log.e("退出登陆==",s);
                        callback.logoutSucess(s);
                    }
                });

    }
    //收藏文章


    public void collect(Context context, String id, final CollectSucessCallback callback){
        OkHttpClient.Builder builder=new OkHttpClient.Builder();
        OkHttpClient client=builder.addInterceptor(new AddCookiesInterceptor(context))
                .build();

        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ConTract.base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();




        CollectApi api=retrofit.create(CollectApi.class);
        api.collect(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
//                        Log.e("收藏文章",e.getMessage().toString());
                        callback.collectFail(e.getMessage());
                    }

                    @Override
                    public void onNext(BaseResponse s) {
//                        Log.e("收藏文章",s.toString());
                        callback.collectSucess(s);
                    }
                });




    }
    //取消收藏

    public void unCollect(Context context, String id, final UnCollectSuccessCallback collectSuccessCallback){

        OkHttpClient.Builder builder=new OkHttpClient.Builder();
        OkHttpClient client=builder.addInterceptor(new AddCookiesInterceptor(context))
                .build();

        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(ConTract.base_url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(client)
                .build();
        UnCollectApi api=retrofit.create(UnCollectApi.class);
        api.unCollect(id).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        collectSuccessCallback.unCollectFail(e.getMessage());
                    }

                    @Override
                    public void onNext(BaseResponse response) {
                        collectSuccessCallback.unCollectSuccess(response);
                    }
                });


    }



}
